package top.mccat.filesystem.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import top.mccat.filesystem.interceptor.AuthenticationInterceptor;
import top.mccat.filesystem.resolver.CurrentAccountMethodArgumentResolver;

import java.util.List;

/**
 * @author JMY
 * @Description 设置虚拟路径
 * @Version 1.0.0
 * @Date 2023/2/24 15:19
 * @Created by JMY
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
    @Autowired
    private AuthenticationInterceptor authenticationInterceptor;
    @Autowired
    private CurrentAccountMethodArgumentResolver currentAccountMethodArgumentResolver;
    //设置文件虚拟路径映射
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**")
                .addResourceLocations("file:D:\\mccat\\uploadPath\\");
    }

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        //对所有的请求进行拦截，判断是否需要登陆 决定是否需要登录操作
        registry.addInterceptor(authenticationInterceptor)
                .addPathPatterns("/file/**")
                .addPathPatterns("/account/list")
                .addPathPatterns("/account/edit")
                .addPathPatterns("/account/delete");
        super.addInterceptors(registry);
    }

    /**
     * 增加参数解析
     * @param argumentResolvers
     */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(currentAccountMethodArgumentResolver);
    }
}
